<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="参考文章：布列瑟农的秋天    https:&#x2F;&#x2F;blog.csdn.net&#x2F;wlwl0071986&#x2F;article&#x2F;details&#x2F;48542923后时代的觉悟   https:&#x2F;&#x2F;blog.csdn.net&#x2F;liqianwei1230&#x2F;article&#x2F;details&#x2F;78606935明朗晨光     Android | 说说Presentation    https:&#x2F;&#x2F;blog.csdn.ne">
<meta property="og:type" content="article">
<meta property="og:title" content="安卓的双屏异显">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E7%9A%84%E5%8F%8C%E5%B1%8F%E5%BC%82%E6%98%BE/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="参考文章：布列瑟农的秋天    https:&#x2F;&#x2F;blog.csdn.net&#x2F;wlwl0071986&#x2F;article&#x2F;details&#x2F;48542923后时代的觉悟   https:&#x2F;&#x2F;blog.csdn.net&#x2F;liqianwei1230&#x2F;article&#x2F;details&#x2F;78606935明朗晨光     Android | 说说Presentation    https:&#x2F;&#x2F;blog.csdn.ne">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-25T14:42:51.180Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.803Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="Android Studio">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E7%9A%84%E5%8F%8C%E5%B1%8F%E5%BC%82%E6%98%BE/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>安卓的双屏异显 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E5%AE%89%E5%8D%93%E7%9A%84%E5%8F%8C%E5%B1%8F%E5%BC%82%E6%98%BE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          安卓的双屏异显
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>参考文章：<br>布列瑟农的秋天    <a target="_blank" rel="noopener" href="https://blog.csdn.net/wlwl0071986/article/details/48542923">https://blog.csdn.net/wlwl0071986/article/details/48542923</a><br>后时代的觉悟   <a target="_blank" rel="noopener" href="https://blog.csdn.net/liqianwei1230/article/details/78606935">https://blog.csdn.net/liqianwei1230/article/details/78606935</a><br>明朗晨光     Android | 说说Presentation    <a target="_blank" rel="noopener" href="https://blog.csdn.net/u011386173/article/details/88717174">https://blog.csdn.net/u011386173/article/details/88717174</a></p>
<h1 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h1><p>在做项目时，有一个双屏显示的需求（类似饭店里面点餐的那种台式安卓机，两面屏幕）。百度一番，发现了 presentation 关键词是实现双屏异显功能的重点。在此记录一下笔记，供后续参考。</p>
<h1 id="官方释义"><a href="#官方释义" class="headerlink" title="官方释义"></a>官方释义</h1><p>如果你英语能力可以的话，官方文档请直接食用：</p>
<h2 id="官方文档"><a href="#官方文档" class="headerlink" title="官方文档"></a>官方文档</h2><blockquote>
<p>A presentation is a special kind of dialog whose purpose is to present content on a secondary display. A Presentation is associated with the target Display at creation time and configures its context and resource configuration according to the display’s metrics.</p>
</blockquote>
<blockquote>
<p>Notably, the Context of a presentation is different from the context of its containing Activity. It is important to inflate the layout of a presentation and load other resources using the presentation’s own context to ensure that assets of the correct size and density for the target display are loaded.</p>
</blockquote>
<blockquote>
<p>A presentation is automatically canceled (see cancel()) when the display to which it is attached is removed. An activity should take care of pausing and resuming whatever content is playing within the presentation whenever the activity itself is paused or resumed.</p>
</blockquote>
<h2 id="我的翻译"><a href="#我的翻译" class="headerlink" title="我的翻译"></a>我的翻译</h2><p>presentation 是一个特殊的 dialog ，主要的目的是在辅助显示屏上显示内容，Presentation 在创建的时候需要和特定的 Display 相关联。 </p>
<p>值得注意的是，presentation 的上下文与包含它的Activity 的上下文不同。重要的是要填充 presentation 的布局，并使用表示本身的上下文加载其他资源，以确保加载了用于目标显示的正确大小和密度的资源。</p>
<p>当presentation 关联的屏幕被移除后，presentation会自动被cancel，当Activity本身pause和resume的时候，Activity应该负责暂停和恢复presentation中的播放的任何内容。</p>
<h2 id="解析总结"><a href="#解析总结" class="headerlink" title="解析总结"></a>解析总结</h2><p>把官方文档总结整理一下，可以得出以下几点结论：</p>
<ul>
<li>presentation 父类是 dialog，所以和dialog具有相似的属性和方法。</li>
<li>presentation 的容器是屏幕，创建时即需指定。屏幕移除，则presentation自动取消。</li>
<li>presentation 的上下文与包含它的Activity 的上下文不同。</li>
</ul>
<h1 id="使用"><a href="#使用" class="headerlink" title="使用"></a>使用</h1><p>demo 示例</p>
<ol>
<li>设置权限<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- 显示系统窗口权限 --&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.SYSTEM_ALERT_WINDOW&quot;</span>/&gt;</span></span><br><span class="line"> <span class="comment">&lt;!-- 在 屏幕最顶部显示addview--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">&quot;android.permission.SYSTEM_OVERLAY_WINDOW&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure></li>
<li>自定义类 继承Presentation</li>
</ol>
<p>辅助显示屏的内容：</p>
<ul>
<li>需要扩展 Presentation 类，并实现 onCreate() 回调方法。在 onCreate() 中，调用setContentView() 来指定您要在辅助显示屏上显示的 UI。 </li>
<li>作为 Dialog 类的扩展，Presentation 类提供了一个区域，其中可以在辅助显示屏上显示不同的 UI。<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DifferentDislay</span> <span class="keyword">extends</span> <span class="title">Presentation</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">       <span class="function"><span class="keyword">public</span> <span class="title">DifferentDislay</span><span class="params">(Context outerContext, Display display)</span> </span>&#123;</span><br><span class="line">           <span class="keyword">super</span>(outerContext,display);</span><br><span class="line"></span><br><span class="line">       &#125;</span><br><span class="line">       <span class="meta">@Override</span></span><br><span class="line">       <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">           <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">           setContentView(R.layout.second_screen);</span><br><span class="line"></span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<p>3.代码实现：<br>根据官网文档，获取显示屏的代码如下;</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">DisplayManager displayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);</span><br><span class="line">Display[] presentationDisplays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);</span><br><span class="line"><span class="keyword">if</span> (presentationDisplays.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">    <span class="comment">// If there is more than one suitable presentation display, then we could consider</span></span><br><span class="line">    <span class="comment">// giving the user a choice.  For this example, we simply choose the first display</span></span><br><span class="line">    <span class="comment">// which is the one the system recommends as the preferred presentation display.</span></span><br><span class="line">    Display display = presentationDisplays[<span class="number">0</span>];</span><br><span class="line">    Presentation presentation = <span class="keyword">new</span> MyPresentation(context, presentationDisplay);</span><br><span class="line">    presentation.show();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> DifferentDislay mPresentation;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);</span><br><span class="line">        Display[] displays = manager.getDisplays();</span><br><span class="line">        <span class="comment">// displays[0] 主屏</span></span><br><span class="line">        <span class="comment">// displays[1] 副屏</span></span><br><span class="line">        DifferentDislay differentDislay = <span class="keyword">new</span> DifferentDislay(<span class="keyword">this</span>,displays[<span class="number">1</span>]);</span><br><span class="line">        differentDislay.getWindow().setType(</span><br><span class="line">                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);</span><br><span class="line">        differentDislay.show();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DifferentDislay</span> <span class="keyword">extends</span> <span class="title">Presentation</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="title">DifferentDislay</span><span class="params">(Context outerContext, Display display)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">super</span>(outerContext,display);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">            <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">            setContentView(R.layout.second_screen);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在辅助显示屏上播放视频：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">AppCompatActivity</span> <span class="keyword">implements</span> <span class="title">MediaPlayer</span>.<span class="title">OnPreparedListener</span>, <span class="title">SurfaceHolder</span>.<span class="title">Callback</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">&quot;Bradlley&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> DifferentDislay mPresentation;</span><br><span class="line">    <span class="keyword">private</span> MediaPlayer mMediaPlayer;</span><br><span class="line">    <span class="keyword">private</span> SurfaceView mSurfaceView;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.activity_main);</span><br><span class="line"></span><br><span class="line">        DisplayManager manager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);</span><br><span class="line">        Display[] displays = manager.getDisplays();</span><br><span class="line">        <span class="comment">// displays[0] 主屏</span></span><br><span class="line">        <span class="comment">// displays[1] 副屏</span></span><br><span class="line">        DifferentDislay differentDislay = <span class="keyword">new</span> DifferentDislay(<span class="keyword">this</span>,displays[<span class="number">1</span>]);</span><br><span class="line">        differentDislay.getWindow().setType(</span><br><span class="line">                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);</span><br><span class="line">        differentDislay.show();</span><br><span class="line"></span><br><span class="line">        mMediaPlayer = <span class="keyword">new</span> MediaPlayer();</span><br><span class="line">        mMediaPlayer.setOnPreparedListener(<span class="keyword">this</span>);</span><br><span class="line"></span><br><span class="line">        mSurfaceView= differentDislay.getSurfaceView();</span><br><span class="line">        mSurfaceView.getHolder().addCallback(<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">surfaceCreated</span><span class="params">(SurfaceHolder holder)</span> </span>&#123;</span><br><span class="line">        Log.d(TAG, <span class="string">&quot;surfaceCreated: &quot;</span>);</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            mMediaPlayer.setDataSource(<span class="string">&quot;/sdcard/Movies/mv.wmv&quot;</span>);</span><br><span class="line">            mMediaPlayer.setDisplay(mSurfaceView.getHolder());</span><br><span class="line">            mMediaPlayer.prepareAsync();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (IOException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">surfaceChanged</span><span class="params">(SurfaceHolder holder, <span class="keyword">int</span> format, <span class="keyword">int</span> width, <span class="keyword">int</span> height)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">surfaceDestroyed</span><span class="params">(SurfaceHolder holder)</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onPrepared</span><span class="params">(MediaPlayer mp)</span> </span>&#123;</span><br><span class="line">        mMediaPlayer.start();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>DifferentDislay 代码片段：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DifferentDislay</span> <span class="keyword">extends</span> <span class="title">Presentation</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String TAG = <span class="string">&quot;DifferentDislay&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> SurfaceView mSurfaceView;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DifferentDislay</span><span class="params">(Context outerContext, Display display)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(outerContext,display);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        Log.d(TAG, <span class="string">&quot;onCreate: &quot;</span>);</span><br><span class="line">        setContentView(R.layout.second_screen);</span><br><span class="line">        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> SurfaceView <span class="title">getSurfaceView</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> mSurfaceView;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="原理探究"><a href="#原理探究" class="headerlink" title="原理探究"></a>原理探究</h1><h2 id="presentation-处在哪个View层级？"><a href="#presentation-处在哪个View层级？" class="headerlink" title="presentation 处在哪个View层级？"></a>presentation 处在哪个View层级？</h2><p>presentation 布局</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PhoneWindows $ DecorView</span><br><span class="line">----LinearLayout</span><br><span class="line">--------Iaction_mode bar_stub（ViewStub）</span><br><span class="line">--------content（FrameLayout）</span><br><span class="line">------------LinearLayout（presentation 布局的根节点）</span><br></pre></td></tr></table></figure>
<p>Dialog布局</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PhoneWindows $ DecorView</span><br><span class="line">---- FrameLayout</span><br><span class="line">---- ---- content（FrameLayout）</span><br><span class="line">---- ---- ---- LinearLayout（presentation 布局的根节点）</span><br><span class="line">---- ---- Iaction_mode bar_stub（ViewStub）</span><br></pre></td></tr></table></figure>
<p>由此可见，presentation和dialog布局层级是一样的</p>
<h1 id="琐碎知识"><a href="#琐碎知识" class="headerlink" title="琐碎知识"></a>琐碎知识</h1><p>1.如何获取设备上的屏幕？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">DisplayManager  mDisplayManager;<span class="comment">//屏幕管理类</span></span><br><span class="line">Display[]  displays;<span class="comment">//屏幕数组</span></span><br><span class="line">mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);</span><br><span class="line">displays =mDisplayManager.getDisplays();</span><br></pre></td></tr></table></figure>

<p>2.主屏和副屏的区分？</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">主屏：displays[<span class="number">0</span>]</span><br><span class="line">副屏：displays[<span class="number">1</span>]</span><br></pre></td></tr></table></figure>

<p>3.如何在副屏上展示内容？</p>
<p>通过Presentation来实现，Presentation继承了Dialog。假设我们写了一个DifferentDislay的类，这个类是要继承<br>Presentation类的。代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">privateclass DifferentDislay extends Presentation&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">DifferentDislay</span><span class="params">(ContextouterContext, Display display)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(outerContext,display);</span><br><span class="line">        <span class="comment">//TODOAuto-generated constructor stub  </span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function">protectedvoid <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</span><br><span class="line">        setContentView(R.layout.layout02);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>4.开启副屏</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DifferentDislay  mPresentation =<span class="keyword">new</span> DifferentDislay (getApplicationContext(),displays[<span class="number">1</span>]);<span class="comment">//displays[1]是副屏</span></span><br><span class="line">mPresentation.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);</span><br><span class="line">mPresentation.show();</span><br></pre></td></tr></table></figure>









    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/Android-Studio/" rel="tag"># Android Studio</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E5%AE%89%E5%8D%93%E6%89%AB%E7%A0%81%E6%9E%AA%E5%BC%80%E5%8F%91%EF%BC%8C%E6%8B%A6%E6%88%AA%E6%89%AB%E7%A0%81%E4%BA%8B%E4%BB%B6/" rel="prev" title="安卓扫码枪开发，拦截扫码事件">
      <i class="fa fa-chevron-left"></i> 安卓扫码枪开发，拦截扫码事件
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E4%BA%8C%EF%BC%89%EF%BC%9A%E5%BA%94%E7%94%A8%E5%90%AF%E5%8A%A8%E5%92%8C%E6%AC%A2%E8%BF%8E%E9%A1%B5%E9%9D%A2/" rel="next" title="手把手带你撸一个校园APP（二）：应用启动和欢迎页面">
      手把手带你撸一个校园APP（二）：应用启动和欢迎页面 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%9C%80%E6%B1%82"><span class="nav-number">1.</span> <span class="nav-text">需求</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%98%E6%96%B9%E9%87%8A%E4%B9%89"><span class="nav-number">2.</span> <span class="nav-text">官方释义</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%98%E6%96%B9%E6%96%87%E6%A1%A3"><span class="nav-number">2.1.</span> <span class="nav-text">官方文档</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%88%91%E7%9A%84%E7%BF%BB%E8%AF%91"><span class="nav-number">2.2.</span> <span class="nav-text">我的翻译</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A7%A3%E6%9E%90%E6%80%BB%E7%BB%93"><span class="nav-number">2.3.</span> <span class="nav-text">解析总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8E%9F%E7%90%86%E6%8E%A2%E7%A9%B6"><span class="nav-number">4.</span> <span class="nav-text">原理探究</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#presentation-%E5%A4%84%E5%9C%A8%E5%93%AA%E4%B8%AAView%E5%B1%82%E7%BA%A7%EF%BC%9F"><span class="nav-number">4.1.</span> <span class="nav-text">presentation 处在哪个View层级？</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%90%90%E7%A2%8E%E7%9F%A5%E8%AF%86"><span class="nav-number">5.</span> <span class="nav-text">琐碎知识</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
